import { createRouter, createWebHistory } from 'vue-router'
import Layout from '@/layout/index.vue'

const router = createRouter({
  history: createWebHistory(),
  routes: [
    {
      path: '/login',
      name: 'Login',
      component: () => import('@/views/login.vue')
    },
    {
      path: '/',
      component: Layout,
      redirect: '/dashboard',
      children: [
        {
          path: 'dashboard',
          name: 'Dashboard',
          component: () => import('@/views/dashboard/index.vue'),
          meta: { title: '仪表盘', icon: 'Odometer' }
        }
      ]
    },
    {
      path: '/asset',
      component: Layout,
      redirect: '/asset/list',
      meta: { title: '资产管理', icon: 'Box' },
      children: [
        {
          path: 'list',
          name: 'AssetList',
          component: () => import('@/views/asset/list.vue'),
          meta: { title: '资产列表', icon: 'List' }
        },
        {
          path: 'add',
          name: 'AssetAdd',
          component: () => import('@/views/asset/add.vue'),
          meta: { title: '添加资产', icon: 'Plus' }
        },
        {
          path: 'edit',
          name: 'AssetEditDefault',
          component: () => import('@/views/asset/edit.vue'),
          meta: { title: '编辑资产', icon: 'Edit' }
        },
        {
          path: 'edit/:id',
          name: 'AssetEdit',
          component: () => import('@/views/asset/edit.vue'),
          meta: { title: '编辑资产', icon: 'Edit' }
        },
        {
          path: 'detail',
          name: 'AssetDetailDefault',
          component: () => import('@/views/asset/detail.vue'),
          meta: { title: '资产详情', icon: 'View' }
        },
        {
          path: 'detail/:id',
          name: 'AssetDetail',
          component: () => import('@/views/asset/detail.vue'),
          meta: { title: '资产详情', icon: 'View' }
        },
        {
          path: 'export',
          name: 'AssetExport',
          component: () => import('@/views/asset/export.vue'),
          meta: { title: '数据导出', icon: 'Download' }
        }
      ]
    },
    {
      path: '/category',
      component: Layout,
      redirect: '/category/list',
      meta: { title: '分类管理', icon: 'Folder' },
      children: [
        {
          path: 'list',
          name: 'CategoryList',
          component: () => import('@/views/category/list.vue'),
          meta: { title: '分类列表', icon: 'List' }
        },
        {
          path: 'add',
          name: 'CategoryAdd',
          component: () => import('@/views/category/add.vue'),
          meta: { title: '添加分类', icon: 'Plus' }
        },
        {
          path: 'edit',
          name: 'CategoryEditDefault',
          component: () => import('@/views/category/edit.vue'),
          meta: { title: '编辑分类', icon: 'Edit' }
        },
        {
          path: 'edit/:id',
          name: 'CategoryEdit',
          component: () => import('@/views/category/edit.vue'),
          meta: { title: '编辑分类', icon: 'Edit' }
        },
        {
          path: 'tree',
          name: 'CategoryTree',
          component: () => import('@/views/category/tree.vue'),
          meta: { title: '分类树形', icon: 'Share' }
        }
      ]
    },
    {
      path: '/maintenance',
      component: Layout,
      redirect: '/maintenance/list',
      meta: { title: '维护管理', icon: 'Tools' },
      children: [
        {
          path: 'list',
          name: 'MaintenanceList',
          component: () => import('@/views/maintenance/list.vue'),
          meta: { title: '维护记录', icon: 'List' }
        },
        {
          path: 'add',
          name: 'MaintenanceAdd',
          component: () => import('@/views/maintenance/add.vue'),
          meta: { title: '添加维护', icon: 'Plus' }
        },
        {
          path: 'schedule',
          name: 'MaintenanceSchedule',
          component: () => import('@/views/maintenance/schedule.vue'),
          meta: { title: '维护计划', icon: 'Calendar' }
        }
      ]
    },
    {
      path: '/user',
      component: Layout,
      redirect: '/user/list',
      meta: { title: '用户管理', icon: 'User' },
      children: [
        {
          path: 'list',
          name: 'UserList',
          component: () => import('@/views/user/list.vue'),
          meta: { title: '用户列表', icon: 'List' }
        },
        {
          path: 'add',
          name: 'UserAdd',
          component: () => import('@/views/user/add.vue'),
          meta: { title: '添加用户', icon: 'Plus' }
        },
        {
          path: 'edit/:id',
          name: 'UserEdit',
          component: () => import('@/views/user/add.vue'),
          meta: { title: '编辑用户', icon: 'Edit' }
        },
        {
          path: 'role',
          name: 'UserRole',
          component: () => import('@/views/user/role.vue'),
          meta: { title: '角色管理', icon: 'Lock' }
        }
      ]
    },
    {
      path: '/system',
      component: Layout,
      redirect: '/system/config',
      meta: { title: '系统设置', icon: 'Setting' },
      children: [
        {
          path: 'config',
          name: 'SystemConfig',
          component: () => import('@/views/system/config.vue'),
          meta: { title: '系统配置', icon: 'Tools' }
        },
        {
          path: 'log',
          name: 'SystemLog',
          component: () => import('@/views/system/log.vue'),
          meta: { title: '操作日志', icon: 'Document' }
        },
        {
          path: 'backup',
          name: 'SystemBackup',
          component: () => import('@/views/system/backup.vue'),
          meta: { title: '数据备份', icon: 'FolderOpened' }
        },
        {
          path: 'help',
          name: 'SystemHelp',
          component: () => import('@/views/system/help.vue'),
          meta: { title: '帮助文档', icon: 'QuestionFilled' }
        },
        {
          path: 'about',
          name: 'SystemAbout',
          component: () => import('@/views/system/about.vue'),
          meta: { title: '关于系统', icon: 'InfoFilled' }
        }
      ]
    }
  ]
})

export default router

// 登录拦截：首次进入需登录
router.beforeEach((to, _from, next) => {
  const loggedIn = sessionStorage.getItem('loggedIn') === '1'
  if (!loggedIn && to.path !== '/login') {
    return next('/login')
  }
  if (loggedIn && to.path === '/login') {
    return next('/')
  }
  next()
}) 